//
//  SecondViewController.m
//  shouji138
//
//  Created by administrator on 8/27/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SecondViewController.h"
#import "SrnTeacherAppDelegate.h"
#import "SwitchViewController.h"
#import "user.h"
#import "QuartzCore/QuartzCore.h"

#define ROW_HEIGHT 60

@implementation SecondViewController

@synthesize myscrollView,dateFormatter,sessionState,count, qcount;
#pragma mark Table View Controller Methods;


-(IBAction)clearButtonClick:(id)sender{
	[[SrnTeacherAppDelegate App].listOfItems removeAllObjects];
	[myscrollView reloadData];
	[udb deleteOneQuestion:currSid];
}

-(IBAction)manageButtonClick:(id)sender{
	
		[self.navigationController presentModalViewController:[SrnTeacherAppDelegate App].viewController.manageviewcontroller animated:YES];
	
}

-(IBAction)reportButtonClick:(id)sender{
	
	//User *user = [User alloc];
	//user.uid = 111;
	//user.username = @"zx";
	//user.choice = @"B";
	//user.num = 222;
	
	//udb =[db alloc];
	//BOOL re = [udb open];
	//BOOL re1 = [udb insertOneChoice:user];
	//[udb close];
	
	
	//[udb delete];
	//[udb getResults:users];
}

-(IBAction)newClick:(id)sender{
	
	[SrnTeacherAppDelegate App].currid++; 
	currSid = [SrnTeacherAppDelegate App].currid;
	[SrnTeacherAppDelegate App].isNew = YES;
	sessionState.text = @"Newed";
	NSLog(@"new clicked!");
	NSString *cc = [NSString stringWithFormat:@"%d", [SrnTeacherAppDelegate App].currid];
	qcount.text=cc;
}

-(IBAction)saveClick:(id)sender{
	//[udb updateFlag:currSid];
	if ([SrnTeacherAppDelegate App].isClose == NO) {
	  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warnning"
													message:@"You Should close current question first!"
														delegate:self
												  cancelButtonTitle:nil
												  otherButtonTitles:@"Ok",nil];
		
	  [alertView show];
	  [alertView release];
	}
	NSLog(@"save clicked!");
	[refile deleteFile];
	NSMutableArray	 *users;
	users = [[NSMutableArray alloc] init];
	[udb getResults:users];
	[udb delete];
	
	MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
	controller.mailComposeDelegate = self;
	[controller setSubject:@"Mindtally Vote Results"];
	[controller setMessageBody:@"Hi \r\n voting results from mindtally" isHTML:NO];
	
	//attach file to the mail.
	//NSString  *paths = [[NSBundle mainBundle] resourcePath];
	//NSString *path = [paths stringByAppendingString:@"results.csv"];
    NSString *path = [refile getPath];
	NSData *myData = [NSData dataWithContentsOfFile:path];
	[controller addAttachmentData:myData mimeType:@"text/csv" fileName:@"results.csv"];
	
	[self presentModalViewController:controller animated:YES];
	[[SrnTeacherAppDelegate App].listOfItems removeAllObjects];
	[SrnTeacherAppDelegate App].currSSid = [SrnTeacherAppDelegate App].currid;
	[controller release];
	
}

-(IBAction)closeClick:(id)sender{
	[SrnTeacherAppDelegate App].isNew = NO;
	[SrnTeacherAppDelegate App].isOpen = NO;
	[SrnTeacherAppDelegate App].isClose = YES;
	[[SrnTeacherAppDelegate App].listOfItems removeAllObjects];
	[myscrollView reloadData];	
	NSLog(@"close clicked!");
	sessionState.text = @"Closed";
	[SrnTeacherAppDelegate App].currVid = 0;
}

-(IBAction)showClick:(id)sender{

	[SrnTeacherAppDelegate App].isHide = NO;
	NSLog(@"show clicked!");
	sessionState.text = @"Show";
}

-(IBAction)hideClick:(id)sender{
	[SrnTeacherAppDelegate App].isHide = YES;
	NSLog(@"hide clicked!");
	sessionState.text = @"Hided";
}

-(IBAction)openClick:(id)sender{
	if ([SrnTeacherAppDelegate App].isNew == NO) {
		UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warnning"
			message:@"You Should new a question first!"
			delegate:self
			cancelButtonTitle:nil
		    otherButtonTitles:@"Ok",nil];
		
	[alertView show];
	[alertView release];	
	}
	[SrnTeacherAppDelegate App].isOpen = YES;
	NSLog(@"open clicked!");
	sessionState.text = @"Opened";
}

-(IBAction)sendMailClick:(id)sender{
	
	[refile deleteFile];
	NSMutableArray	 *users;
	users = [[NSMutableArray alloc] init];
	[udb getResults:users];
	[udb delete];
	
	MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
	controller.mailComposeDelegate = self;
	[controller setSubject:@"Mindtally Vote Results"];
	[controller setMessageBody:@"Hi \r\n voting results from mindtally" isHTML:NO];
	
	//attach file to the mail.
	//NSString  *paths = [[NSBundle mainBundle] resourcePath];
	//NSString *path = [paths stringByAppendingString:@"results.csv"];
    NSString *path = [refile getPath];
	NSData *myData = [NSData dataWithContentsOfFile:path];
	[controller addAttachmentData:myData mimeType:@"text/csv" fileName:@"results.csv"];
	
	[self presentModalViewController:controller animated:YES];
	[controller release];
	
	//[myData release];
}

-(IBAction)viewQuestionsClick:(id)sender{

	[self.navigationController presentModalViewController:[SrnTeacherAppDelegate App].viewController.studentsQuestionViewController animated:YES];
}

-(IBAction)viewHandsRaisedClick:(id)sender{
	[self.navigationController presentModalViewController:[SrnTeacherAppDelegate App].viewController.handsRaisedViewController animated:YES];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
	[self becomeFirstResponder];
	[self dismissModalViewControllerAnimated:YES];
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 -(void)viewDidLoad {
	 //NSArray *array = [[NSArray alloc] initWithObjects:@"one", @"two",@"three",@"four",
	//				   @"none", @"ntwo",@"nthree",@"nfour",@"nnone", @"nntwo",@"nnthree",@"nnfour"];
	// self.listData = array;
	 [super viewDidLoad];
	 
	 self.view.backgroundColor = [UIColor lightGrayColor];
	 
	 ip.text = [SrnTeacherAppDelegate App].ipaddress;
	 port.text = [NSString stringWithFormat:@"%hu",[SrnTeacherAppDelegate App].port];
	 count.text = @"0";
	 
	 //getTime
	 self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	 [self.dateFormatter setDateStyle:NSDateFormatterNoStyle];
	 [self.dateFormatter setTimeStyle:NSDateFormatterShortStyle];
	 startedTime.text = [self.dateFormatter stringFromDate:[NSDate date]];
	 
	 if ([MFMailComposeViewController canSendMail])
		 sendMailButton.enabled = YES;	
	 
	 udb =[db alloc];
	 BOOL re = [udb open];
	 currSid = [udb getMaxId];
	 if (currSid < 0) {
		 currSid = 0;
	 }
	 [SrnTeacherAppDelegate App].udb = udb;
	 [SrnTeacherAppDelegate App].currid =  currSid;
	 refile = [file alloc];
	 sessionState.text = @"Closed";
	 myscrollView.hidden = YES;
	
	 [SrnTeacherAppDelegate App].viewController.secondviewcontroller = self;
	 [SrnTeacherAppDelegate App].isClose = YES;
	 [SrnTeacherAppDelegate App].isOpen = NO;
	 [SrnTeacherAppDelegate App].isNew = NO;
	 myscrollView.layer.cornerRadius = 8;
	 myscrollView.layer.masksToBounds = YES;
	 int curr_mixID = [udb getMinId] - 1 ;
	 if (curr_mixID < 0 )  curr_mixID = 0;
	 [SrnTeacherAppDelegate App].currSSid = curr_mixID ;
 }

//-(NSString *)getCurrentTime{
//	NSDateFormatterShortStyle
//}

-(void)updateUser
{
	count.text = [NSString stringWithFormat:@"%hu",[SrnTeacherAppDelegate App].userCount];
}



@end
